package com.xuelang.pipeline.entry.application;

import com.xuelang.pipeline.app.application.ApplicationService;
import com.xuelang.pipeline.client.application.IApplicationService;
import com.xuelang.pipeline.client.application.dto.ApplicationComponentDTO;
import com.xuelang.pipeline.client.application.dto.ApplicationDTO;
import com.xuelang.pipeline.client.application.dto.ApplicationQuery;
import com.xuelang.pipeline.client.application.dto.MyApplicationDTO;
import com.xuelang.pipeline.client.common.constant.Constant;
import com.xuelang.pipeline.client.common.request.PageParam;
import com.xuelang.pipeline.client.common.result.ApiResult;
import com.xuelang.pipeline.client.common.result.PageRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import java.util.List;

/**
 * 应用服务web控制类
 *
 * @author liuyuan.ly
 * @since 2024/5/14 9:54
 */
@Slf4j
@RestController
@RequestMapping("/application")
public class ApplicationController implements IApplicationService {

    @Autowired
    private ApplicationService applicationService;

    @Override
    public ApiResult<PageRecord<ApplicationDTO>> pageApplicationList(PageParam<ApplicationQuery> dtoPageParam) {
        return ApiResult.success(applicationService.pageList(dtoPageParam));
    }

    @Override
    public ApiResult<PageRecord<MyApplicationDTO>> pageMyApplications(@Valid PageParam<ApplicationQuery> dtoPageParam) {
        return ApiResult.success(applicationService.pageMyApps(dtoPageParam));
    }

    @Override
    public ApiResult<List<ApplicationComponentDTO>> applicationListWithComponent(ApplicationQuery applicationQuery) {
        return ApiResult.success(applicationService.listWithComponent(applicationQuery));
    }

    @Override
    public ApiResult<List<ApplicationComponentDTO>> applicationListWithComponentPermissions(HttpServletRequest request, ApplicationQuery applicationQuery) {
        applicationQuery.setProjectCode(request.getHeader(Constant.PROJECT_CODE_HEADER_KEY));
        return ApiResult.success(applicationService.applicationListWithComponentPermissions(applicationQuery));
    }

    @Override
    public ApiResult<List<ApplicationDTO>> listAppByKeyword(String keyword) {
        return ApiResult.success(applicationService.list(ApplicationQuery.builder().name(keyword).build()));
    }

    @Override
    public ApiResult<Boolean> addApplication(HttpServletRequest request, ApplicationDTO applicationDTO) {
        String projectCode = request.getHeader(Constant.PROJECT_CODE_HEADER_KEY);
        applicationDTO.setProjectCode(projectCode);
        return ApiResult.success(applicationService.add(applicationDTO));
    }

    @Override
    public ApiResult<Boolean> updateApplication(HttpServletRequest request, ApplicationDTO applicationDTO) {
        String projectCode = request.getHeader(Constant.PROJECT_CODE_HEADER_KEY);
        applicationDTO.setProjectCode(projectCode);
        return ApiResult.success(applicationService.update(applicationDTO));
    }

    @Override
    public ApiResult<Boolean> deleteApplication(String code) {
        return ApiResult.success(applicationService.delete(code));
    }

    @Override
    public ApiResult<ApplicationComponentDTO> queryApplication(String code) {
        return ApiResult.success(applicationService.query(code));
    }

}
